import { Injectable } from "@angular/core";
import { forkJoin, of as observableOf, BehaviorSubject } from "rxjs";
import { FetchService } from "src/app/core/net/fetch.service";
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: "root"
})
export class OneMapService {
  constructor(private fetchService: FetchService) { }

  getSearchList(searchKey) {
    return this.fetchService.post("base/wrsBuild/SearchList", {
      buildName: searchKey
    });
  }

  getProcessListDam(data) {
    return this.fetchService.post(
      "base/wrsBrwaterReservior/queryProcessLinList",
      data
    );
  }

  getProcessListPump(data) {
    return this.fetchService.post("base/wrsBrpump/queryProcessLinList", data);
  }

  getMonthListPump(data) {
    return this.fetchService.post("base/wrsBrpump/queryMonthList", data);
  }

  getProcessListGate(data) {
    return this.fetchService.post(
      "base/wrsBrwaterDam/queryProcessLinList",
      data
    );
  }

  getMonthListGate(data) {
    return this.fetchService.post("base/wrsBrwaterDam/queryMonthList", data);
  }

  getProcessListSection(data) {
    return this.fetchService.post(
      "base/wrsBrwaterCannal/queryProcessLinList",
      data
    );
  }

  getMonthListSection(data) {
    return this.fetchService.post("base/wrsBrwaterCannal/queryMonthList", data);
  }

  getDispatchMonData(data) {
    return this.fetchService.post("schedule/actualDispatchMonData/data", data);
  }

  queryOffice(data) {
    return this.fetchService.post("wrs/wrsBmanage/list", data);
  }

  queryCanal(data) {
    return this.fetchService.post("wrs/wrsBtcanalDetail/list", data);
  }

  querySection(data) {
    return this.fetchService.post("wrs/wrsBrcrossSection/list", data);
  }

  queryPaishuigou(data) {
    return this.fetchService.post("wrs/wrsBrdrainageDitch/list", data);
  }

  queryBengzhan(data) {
    return this.fetchService.post("wrs/wrsBpsB/list", data);
  }
  queryDucao(data) {
    return this.fetchService.post("wrs/wrsBtflume/list", data);
  }
  queryHandong(data) {
    return this.fetchService.post("wrs/wrsBtculvert/list", data);
  }
  queryYiliuyan(data) {
    return this.fetchService.post("wrs/wrsBroverflowWeir/list", data);
  }
  queryGate(data) {
    return this.fetchService.post("wrs/wrsBawaterGate/list", data);
  }
  queryZhikaikou(data) {
    return this.fetchService.post("base/wrsBastraightOpening/list", data);
  }
  queryBridge(data) {
    return this.fetchService.post("wrs/wrsBridge/list", data);
  }
  queryDieshui(data) {
    return this.fetchService.post("wrs/wrsBtdrop/list", data);
  }
  queryShanhonggou(data) {
    return this.fetchService.post("base/wrsBrfloodDitch/list", data);
  }
  queryZhihongqu(data) {
    return this.fetchService.post("base/wrsBrfloodDetention/list", data);
  }
  queryIrrArea(data) {
    return this.fetchService.post("wrs/wrsBdistrict/list", data);
  }
  queryGyysdw(data) {
    return this.fetchService.post("base/wrsIndustryCompany/list", data);
  }
  // 基本信息列表
  // 地表水/地下水取水口
  queryIntake(data) {
    return this.fetchService.post<any>('intake/wrsWiuSt/list', data);
  }
  // 水库水文站/雨量站/干渠断面/山洪沟断面/排水沟断面/墒情站/河道测水断面/黄河断面/堰闸水文站基础数据
  querySectionList(data) {
    return this.fetchService.post<any>('wrs/wrsBrstadia/listByPage', data);
  }
  // 取水口-闸
  queryZQSK(data) {
    return this.fetchService.post<any>('witis/aZlqsk/list', data);
  }
  // 取水口-水井
  querySJQSK(data) {
    return this.fetchService.post<any>('witis/aSjlqsk/list', data);
  }
  // 取水口-水电站
  querySDZQSK(data) {
    return this.fetchService.post<any>('witis/aSdzlqsk/list', data);
  }
  // 取水口-其他
  queryQTQSK(data) {
    return this.fetchService.post<any>('witis/aQtlqsk/list', data);
  }
  // 取水口-渠道
  queryQDQSK(data) {
    return this.fetchService.post<any>('witis/aQdlqsk/list', data);
  }
  // 取水口-泵站
  queryBZQSK(data) {
    return this.fetchService.post<any>('witis/aBzlqsk/list', data);
  }
  // 取水口-坝
  queryBQSK(data) {
    return this.fetchService.post<any>('witis/aBlqsk/list', data);
  }

  queryBaseList(opt) {
    let dataObservable;
    switch (opt.type) {
      case "irrArea":
        dataObservable = this.queryIrrArea(opt.params);
        break;
      case "office":
        dataObservable = this.queryOffice({
          ...opt.params,
          type: 1
        });
        break;
      case "station":
        dataObservable = this.queryOffice({
          ...opt.params,
          type: 2
        });
        break;
      case "ganqu":
        dataObservable = this.queryCanal({
          ...opt.params,
          canalType: "02"
        });
        break;
      case "section":
        dataObservable = this.querySection(opt.params);
        break;
      case "paishuigou":
        dataObservable = this.queryPaishuigou(opt.params);
        break;
      case "jinshuizha":
        dataObservable = this.queryGate({
          ...opt.params,
          watergateType: 1
        });
        break;
      case "jiezhizha":
        dataObservable = this.queryGate({
          ...opt.params,
          watergateType: 4
        });
        break;
      case "tuishuizha":
        dataObservable = this.queryGate({
          ...opt.params,
          watergateType: 2
        });
        break;
      case "zhikaikou":
        dataObservable = this.queryZhikaikou(opt.params);
        break;
      case "ducao":
        dataObservable = this.queryDucao(opt.params);
        break;
      case "handong":
        dataObservable = this.queryHandong(opt.params);
        break;
      case "bengzhan":
        dataObservable = this.queryBengzhan(opt.params);
        break;
      case "qiaoliang":
        dataObservable = this.queryBridge(opt.params);
        break;
      case "dieshui":
        dataObservable = this.queryDieshui(opt.params);
        break;
      case "yiliuyan":
        dataObservable = this.queryYiliuyan(opt.params);
        break;
      case "shanhonggou":
        dataObservable = this.queryShanhonggou(opt.params);
        break;
      case "zhihongqu":
        dataObservable = this.queryZhihongqu(opt.params);
        break;
      case "gyysdw":
        dataObservable = this.queryGyysdw(opt.params);
        break;
      case "db":
        dataObservable = this.queryIntake({ ...opt.params, wiustTp: "1" });
        break;
      case "dx":
        dataObservable = this.queryIntake({ ...opt.params, wiustTp: "2" });
        break;
      case "gy":
        dataObservable = this.queryIntake({ ...opt.params, userType: "1" });
        break;
      case "ny":
        dataObservable = this.queryIntake({ ...opt.params, userType: "2" });
        break;
      case "sh":
        dataObservable = this.queryIntake({ ...opt.params, userType: "3" });
        break;
      case "hd":
        dataObservable = this.querySectionList({ ...opt.params, stadiaType: "11" });
        break;
      case "yz":
        dataObservable = this.querySectionList({ ...opt.params, stadiaType: "6" });
        break;
      case "skswz":
        dataObservable = this.querySectionList({ ...opt.params, stadiaType: "4" });
        break;
      case "ylz":
        dataObservable = this.querySectionList({ ...opt.params, stadiaType: "1" });
        break;
      case "sqz":
        dataObservable = this.querySectionList({ ...opt.params, stadiaType: "10" });
        break;
      case "hh":
        dataObservable = this.querySectionList({ ...opt.params, stadiaType: "3,7" });
        break;
      case "gqdm":
        dataObservable = this.querySectionList({ ...opt.params, stadiaType: "2" });
        break;
      case "shg":
        dataObservable = this.querySectionList({ ...opt.params, stadiaType: "5" });
        break;
      case "psg":
        dataObservable = this.querySectionList({ ...opt.params, stadiaType: "8" });
        break;
      case "zlqsk":
        dataObservable = this.queryZQSK(opt.params);
        break;
      case "qdlqsk":
        dataObservable = this.queryQDQSK(opt.params);
        break;
      case "bzlqsk":
        dataObservable = this.queryBZQSK(opt.params);
        break;
      case "sjlqsk":
        dataObservable = this.querySJQSK(opt.params);
        break;
      case "blqsk":
        dataObservable = this.queryBQSK(opt.params);
        break;
      case "sdzlqsk":
        dataObservable = this.querySDZQSK(opt.params);
        break;
      case "qtlqsk":
        dataObservable = this.queryQTQSK(opt.params);
        break;
      default:
        dataObservable = observableOf({});
        break;
    }
    return dataObservable;
  }

  getStaticData(data) {
    return this.fetchService.post(
      "schedule/wrsActualDispatchApply/staticData",
      data
    );
  }

  getStatisList(data) {
    return this.fetchService.post("statis/ohr/list", data);
  }

  getStatisMuw() {
    return this.fetchService.get("statis/muw/query");
  }

  getWarningList() {
    return this.fetchService.post<any>('schedule/actualDispatchWarning/list', {});
  }

  // 实时监测信息
  // 取水口
  queryQSK(data) {
    return this.fetchService.post<any>('intake/wrsWiuSt/query', data);
  }
  // 水库水文站
  querySKSWZ(data) {
    return this.fetchService.post<any>('intake/wrsRsvrR/query', data);
  }
  // 雨量站
  queryYLZ(data) {
    return this.fetchService.post<any>('intake/wrsPptnR/query', data);
  }
  // 墒情站
  querySQZ(data) {
    return this.fetchService.post<any>('intake/wrsSoilR/query', data);
  }
  // 干渠断面
  queryGQDM(data) {
    return this.fetchService.post<any>('base/wrsBrwaterCannal/query', data);
  }
  // 山洪沟断面
  querySHGDM(data) {
    return this.fetchService.post<any>('intake/wrsBrwaterDitch/query', data);
  }
  // 排水沟断面
  queryPSGDM(data) {
    return this.fetchService.post<any>('base/wrsBrwaterDrainage/query', data);
  }
  // 黄河断面 //河道测水断面 // 堰闸水文站
  queryHDDM(data) {
    return this.fetchService.post<any>('base/wrsBrwaterRiver/query', data);
  }
  queryHDDMSK(data) {
    return this.fetchService.post<any>('base/wrsBrwaterReservior/query', data);
  }

  // 基本信息
  // 地表水/地下水取水口基本信息
  getIntakeInfo(id) {
    return this.fetchService.get<any>(`intake/wrsWiuSt/${id}`);
  }
  getIntakeInfo2(id) {
    return this.fetchService.get<any>(`intake/wrsWiuU/${id}`);
  }
  // 水库水文站/雨量站/干渠断面/山洪沟断面/排水沟断面/墒情站/河道测水断面/黄河断面/堰闸水文站基础数据
  getSectionInfo(stadiaCode) {
    return this.fetchService.get<any>(`wrs/wrsBrstadia/${stadiaCode}`);
  }
  // 取水口-闸
  getInfoZQSK(id) {
    return this.fetchService.get<any>(`witis/aZlqsk/${id}`);
  }
  // 取水口-水井
  getInfoSJQSK(id) {
    return this.fetchService.get<any>(`witis/aSjlqsk/${id}`);
  }
  // 取水口-水电站
  getInfoSDZQSK(id) {
    return this.fetchService.get<any>(`witis/aSdzlqsk/${id}`);
  }
  // 取水口-其他
  getInfoQTQSK(id) {
    return this.fetchService.get<any>(`witis/aQtlqsk/${id}`);
  }
  // 取水口-渠道
  getInfoQDQSK(id) {
    return this.fetchService.get<any>(`witis/aQdlqsk/${id}`);
  }
  // 取水口-泵站
  getInfoBZQSK(id) {
    return this.fetchService.get<any>(`witis/aBzlqsk/${id}`);
  }
  // 取水口-坝
  getInfoBQSK(id) {
    return this.fetchService.get<any>(`witis/aBlqsk/${id}`);
  }

  queryMonitorList(opt) {
    let dataObservable;
    switch (opt.type) {
      case "jinshuizha":
        dataObservable = this.queryGate({
          ...opt.params,
          watergateType: 1
        });
        break;
      case "jiezhizha":
        dataObservable = this.queryGate({
          ...opt.params,
          watergateType: 4
        });
        break;
      case "tuishuizha":
        dataObservable = this.queryGate({
          ...opt.params,
          watergateType: 2
        });
        break;
      case "zhikaikou":
        dataObservable = this.queryZhikaikou(opt.params);
        break;
      case "bengzhan":
        dataObservable = this.queryBengzhan(opt.params);
        break;
      case "db":
        dataObservable = this.getMonitorQSK({ ...opt.params, wiustTp: "1" });
        break;
      case "dx":
        dataObservable = this.getMonitorQSK({ ...opt.params, wiustTp: "2" });
        break;
      case "gy":
        dataObservable = this.getMonitorQSK({ ...opt.params, userType: "1" });
        break;
      case "ny":
        dataObservable = this.getMonitorQSK({ ...opt.params, userType: "2" });
        break;
      case "sh":
        dataObservable = this.getMonitorQSK({ ...opt.params, userType: "3" });
        break;
      case "gyjc":
        dataObservable = this.getMonitorQSK2({ ...opt.params, userType: "1", isStat: "1" });
        break;
      case "nyjc":
        dataObservable = this.getMonitorQSK2({ ...opt.params, userType: "2", isStat: "1" });
        break;
      case "shjc":
        dataObservable = this.getMonitorQSK2({ ...opt.params, userType: "3", isStat: "1" });
        break;
      case "hhjc":
        dataObservable = this.getMonitorQSK2({ ...opt.params, userType: "6", isStat: "1" });
        break;
      case "hd":
        dataObservable = this.getMonitorRiver({ ...opt.params, stadiaType: "11" });
        break;
      case "yz":
        dataObservable = this.getMonitorRiver({ ...opt.params, stadiaType: "6" });
        break;
      case "skswz":
        dataObservable = this.getMonitorSKSWZ(opt.params);
        break;
      case "ylz":
        dataObservable = this.getMonitorYLZ(opt.params);
        break;
      case "cyylz":
        dataObservable = this.getMonitorYLZ({
          ...opt.params,
          stazt: 1
        });
        break;
      case "sqz":
        dataObservable = this.getMonitorSQZ(opt.params);
        break;
      case "hh":
        dataObservable = forkJoin(this.getMonitorRiver({ ...opt.params, stadiaType: "7" }), this.getMonitorReservior(opt.params)).pipe(map((res) => {
          const river = res[0]['list'];
          const reservior = res[1]['list'];
          return {
            total: res[0]['total'] + res[1]['total'],
            list: reservior.concat(river)
          };
        }));
        // this.getMonitorRiver({ ...opt.params, stadiaType: "7" });
        break;
      case "gqdm":
        dataObservable = this.getMonitorGQDM(opt.params);
        break;
      case "zygq":
        dataObservable = forkJoin(this.getMonitorGQDM({ ...opt.params, usfl: 1 }), this.getMonitorPump({ ...opt.params, otherType: 1 })).pipe(map((res: any) => {
          const gqdm = res[0]['list'];
          const zybz = res[1]['list'];
          const order = ['唐徕渠', '西干渠', '惠农渠', '汉延渠', '大清渠', '泰民渠', '秦渠', '汉渠', '马莲渠', '东干渠', '七星渠', '跃进渠', '北干渠', '角渠', '寿渠'];
          const newList = [];
          order.map(name => {
            gqdm.map(item => {
              if (item.stadiaName == name) {
                newList.push(item);
              }
            });
          });
          return {
            total: res[0]['total'] + res[1]['total'],
            list: newList.concat(zybz)
          };
        }));
        break;
      case "shg":
        dataObservable = this.getMonitorSHGDM(opt.params);
        break;
      case "psg":
        dataObservable = this.getMonitorPSGDM(opt.params);
        break;
      case "zypsg":
        dataObservable = this.getMonitorPSGDM({ ...opt.params, usfl: 1 });
        break;
      case "zybz":
        dataObservable = this.getMonitorPump({ ...opt.params, otherType: 1 });
        break;
      case "zyzm":
        const mapper = {};
        const list = [];
        // 闸前水位1 闸后水位2 流量3 闸门开启高度4
        const attrs = ['', 'heightUp', 'heightDown', 'flux', 'degree'];
        dataObservable = this.getMonitorGate({ ...opt.params, structureType: 1, pageSize: 100 }).pipe(map(res => {
          res['list'].map(item => {
            const type = Number(item.type);
            if (!mapper[item.strobeCode]) {
              mapper[item.strobeCode] = {
                ...item,
                heightUp: '',
                heightDown: '',
                flux: '',
                degree: []
              };
            }
            if (type == 4) {
              mapper[item.strobeCode][attrs[item.type]][item.num - 1] = item.val;
            } else {
              mapper[item.strobeCode][attrs[item.type]] = item.val;
            }
          });
          for (const key in mapper) {
            if (mapper.hasOwnProperty(key)) {
              const data = mapper[key];
              list.push({
                ...data,
                degreeMax: Math.max.apply(null, data.degree),
                validStrobe: `${data.degree.filter(d => d > 0.001).length}/${data.degree.length}`
              });
            }
          }
          return {
            total: list.length,
            list
          };
        }));
        break;
      default:
        dataObservable = observableOf({});
        break;
    }
    return dataObservable;
  }

  getMonitorGQDM(data) {
    return this.fetchService.post("base/wrsBrwaterCannal/listLast", data);
  }
  getMonitorPSGDM(data) {
    return this.fetchService.post("base/wrsBrwaterDrainage/listLast", data);
  }
  getMonitorRiver(data) {
    return this.fetchService.post("base/wrsBrwaterRiver/listLast", data);
  }
  getMonitorReservior(data) {
    return this.fetchService.post("base/wrsBrwaterReservior/listLast", data);
  }
  getMonitorSHGDM(data) {
    return this.fetchService.post("intake/wrsBrwaterDitch/listLast", data);
  }
  getMonitorSKSWZ(data) {
    return this.fetchService.post("intake/wrsRsvrR/listLast", data);
  }
  getMonitorYLZ(data) {
    return this.fetchService.post("intake/wrsPptnR/listLast", data);
  }
  getMonitorSQZ(data) {
    return this.fetchService.post("intake/wrsSoilR/listLast", data);
  }
  getMonitorQSK(data) {
    return this.fetchService.post("intake/wrsWiuSt/listLast", data);
  }
  getMonitorQSK2(data) {
    return this.fetchService.post("intake/wrsWiuU/listLast", data);
  }
  getMonitorPump(data) {
    return this.fetchService.post("base/wrsBrwaterBpsb/listLast", data);
  }
  getMonitorGate(data) {
    return this.fetchService.post("base/wrsBrwaterGate/listLast", data);
  }

  queryProcessGate(data) {
    // { "strobeCode": "HN-1-QJ1", "beginTime": "2020-06-26", "endTime": "2020-07-01" }
    return this.fetchService.post("base/wrsBrwaterGate/query", data);
  }
  queryProcessPump(data) {
    // { "pumpCode": "YH-1-GS", "beginTime": "2020-06-26", "endTime": "2020-07-01" }
    return this.fetchService.post("base/wrsBrwaterBpsb/query", data);
  }
  queryProcessIntake(data) {
    // {"wiuCd":"710664792","beginTime":"2020-06-26","endTime":"2020-07-01"}
    return this.fetchService.post("intake/wrsWiuU/queryByWiuCd", data);
  }

  Area(p0, p1, p2) {
    let area = 0.0;
    area =
      p0[0] * p1[1] +
      p1[0] * p2[1] +
      p2[0] * p0[1] -
      p1[0] * p0[1] -
      p2[0] * p1[1] -
      p0[0] * p2[1];
    return area / 2;
  }

  getPolygonAreaCenter(points) {
    let sumX = 0;
    let sumY = 0;
    let sumArea = 0;
    let p1 = points[1];
    for (let i = 2; i < points.length; i++) {
      const p2 = points[i];
      const area = this.Area(points[0], p1, p2);
      sumArea += area;
      sumX += (points[0][0] + p1[0] + p2[0]) * area;
      sumY += (points[0][1] + p1[1] + p2[1]) * area;
      p1 = p2;
    }
    const xx = sumX / sumArea / 3;
    const yy = sumY / sumArea / 3;
    return [xx, yy];
  }

  getLineCenter(coordinates) {
    if (coordinates) {
      const length = coordinates.length;
      const centeridx = Math.floor(length / 2);
      return coordinates[centeridx];
    }
    return [];
  }
}
